package com.sinosoft.cnmuseum.servlets;

import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.sinosoft.cnmuseum.dao.MbXtglJgDao;
import com.sinosoft.cnmuseum.dao.MbXtglYhxxDao;
import com.sinosoft.cnmuseum.entity.MbXtglJg;
import com.sinosoft.cnmuseum.entity.MbXtglYhxx;
import com.sinosoft.cnmuseum.utils.LogUtils;
import com.sinosoft.cnmuseum.utils.MD5;
import com.sinosoft.cnmuseum.utils.Status;
import com.sinosoft.cnmuseum.utils.StringUtils;
import com.sinosoft.commontools.annotated.rquestMethod;
import com.sinosoft.commontools.annotated.rquestMethod.ReqMode;
import com.sinosoft.commontools.record.Record;

public class LoginServlet {

	@rquestMethod(MethodNote = ReqMode.Get)
	public Record getUser(HttpServletRequest request, HttpServletResponse response, Record data){
		Record record = new Record();
		record.add(Status.SATUS, Status.OK);
		try {
			HttpSession session = request.getSession(false);
			String userid = (String) session.getAttribute(Status.SESSION_USERID);
			String username = (String) session.getAttribute(Status.SESSION_USERNAME);
			record.add("xm", username);
			MbXtglYhxxDao dao = new MbXtglYhxxDao();
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put(MbXtglYhxx.ZDBH, userid);
			List<Record> list = dao.getListByProperty(map);
			if(list!=null&&list.size()>0){
				Record user = list.get(0);
				int szjg = user.getIntValue(MbXtglYhxx.SZJG);
				int sfwzj = user.getIntValue(MbXtglYhxx.SFWZJ);
				// 设置用户信息
				if(sfwzj==1){
					// 专家标识
					record.add("jg", "专家");
				}else{
					try {
						Record jg = new MbXtglJgDao().getById(szjg);
						String jgmc = jg.getStrValue(MbXtglJg.JGMC);
						if(StringUtils.isNoEmpty(jgmc)){
							record.add("jg", jgmc);
						}
					} catch (Exception e) {
					}
				}
			}
		} catch (Exception e) {
			record.add(Status.SATUS, Status.FAILED);
			record.add(Status.MSG, "未登录！");
		}
		return record;
	}
	
	@rquestMethod(MethodNote = ReqMode.Get)
    public Record getUserInfo(HttpServletRequest request, HttpServletResponse response, Record data){
        Record record = new Record();
        record.add(Status.SATUS, Status.OK);
        try {
            HttpSession session = request.getSession(false);
            String userid = (String) session.getAttribute(Status.SESSION_USERID);
            String username = (String) session.getAttribute(Status.SESSION_USERNAME);
            record.add("xm", username);
            record.add("userid", userid);
            MbXtglYhxxDao dao = new MbXtglYhxxDao();
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put(MbXtglYhxx.ZDBH, userid);
            List<Record> list = dao.getListByProperty(map);
            if(list!=null&&list.size()>0){
                Record user = list.get(0);
                int szjg = user.getIntValue(MbXtglYhxx.SZJG);
                int sfwzj = user.getIntValue(MbXtglYhxx.SFWZJ);
                // 设置用户信息
                if(sfwzj==1){
                    // 专家标识
                    record.add("jg", "专家");
                }else{
                    try {
                        Record jg = new MbXtglJgDao().getById(szjg);
                        String jgmc = jg.getStrValue(MbXtglJg.JGMC);
                        if(StringUtils.isNoEmpty(jgmc)){
                            record.add("jg", jgmc);
                        }
                    } catch (Exception e) {
                    }
                }
            }
            record.add("qxlist", new MbXtglQxServlet().getQxlistByUserid(userid));
        } catch (Exception e) {
            record.add(Status.SATUS, Status.FAILED);
            record.add(Status.MSG, "未登录！");
        }
        return record;
    }
	
	@rquestMethod(MethodNote = ReqMode.Get)
	public Record login(HttpServletRequest request, HttpServletResponse response, Record data){
		Record record = new Record();
		record.add(Status.SATUS, Status.OK);
		try {
			HttpSession session = request.getSession(false);
			String session_code = (String) session.getAttribute(Status.SESSION_VALIDATE_CODE_KEY);
			String code = data.getStrValue("code");code = session_code;
			if(session_code.equals(code)){ 
				String username = data.getStrValue("username");
				String password = data.getStrValue("password");
				MbXtglYhxxDao dao = new MbXtglYhxxDao();
				HashMap<String, Object> map = new HashMap<String, Object>();
				map.put(MbXtglYhxx.XM, username);
				map.put(MbXtglYhxx.YHMM, new MD5().getMD5(password));
				List<Record> list = dao.getListByProperty(map);
				if(list!=null&&list.size()>0){
					Record user = list.get(0);
					String xm = user.getStrValue(MbXtglYhxx.XM);
					String zdbh = user.getStrValue(MbXtglYhxx.ZDBH);
					int szjg = user.getIntValue(MbXtglYhxx.SZJG);
					int sfwzj = user.getIntValue(MbXtglYhxx.SFWZJ);
					// 设置session
					session.setAttribute(Status.SESSION_USERID, zdbh);
					session.setAttribute(Status.SESSION_USERNAME, xm);
					session.setAttribute(Status.USER, xm);
					session.setAttribute(Status.USERID, zdbh);
					session.setAttribute(Status.LOGINTICKET, zdbh);
					session.setAttribute(Status.PASSED, true);
					// 设置用户信息
					if(sfwzj==1){
						// 专家标识
						record.add("jg", "专家");
					}else{
						// 非专家
						record.add("jg", "未知");
						try {
							Record jg = new MbXtglJgDao().getById(szjg);
							String jgmc = jg.getStrValue(MbXtglJg.JGMC);
							if(StringUtils.isNoEmpty(jgmc)){
								record.add("jg", jgmc);
							}
						} catch (Exception e) {
						}
					}
					record.add("xm", xm);
	                LogUtils logUtils = new LogUtils();
	                String desc = LogUtils.MbXtglYhxx_LOGIN_DESC;
	                logUtils.addLog(String.valueOf(LogUtils.type_login), LogUtils.model_yhgl, zdbh, desc);
				}else{
					record.add(Status.SATUS, Status.FAILED);
					record.add(Status.MSG, "用户名或密码错误！");
	                LogUtils logUtils = new LogUtils();
	                logUtils.addLog(String.valueOf(LogUtils.type_login), LogUtils.model_yhgl,null, "用户名或密码错误！");
				}
			}else{
				record.add(Status.SATUS, Status.FAILED);
				record.add(Status.MSG, "验证码错误！");
                LogUtils logUtils = new LogUtils();
                logUtils.addLog(String.valueOf(LogUtils.type_login), LogUtils.model_yhgl,null, "验证码错误！");
			}
		} catch (Exception e) {
			record.add(Status.SATUS, Status.FAILED);
			record.add(Status.MSG, "用户名或密码错误！");
            LogUtils logUtils = new LogUtils();
            logUtils.addLog(String.valueOf(LogUtils.type_login), LogUtils.model_yhgl,null, "用户名或密码错误！");
		}
		return record;
	}
	
	@rquestMethod(MethodNote = ReqMode.Get)
	public Record logout(HttpServletRequest request, HttpServletResponse response, Record data){
		Record record = new Record();
		record.add(Status.SATUS, Status.OK);
		try {
			HttpSession session = request.getSession(false);
			String contributor = (String) session.getAttribute(Status.SESSION_USERID);//责任者
			session.invalidate();
            LogUtils logUtils = new LogUtils();
            String desc = LogUtils.MbXtglYhxx_LOGOUT_DESC;
            logUtils.addLog(String.valueOf(LogUtils.type_logout), LogUtils.model_yhgl, contributor, desc);
		} catch (Exception e) {
			record.add(Status.SATUS, Status.FAILED);
			record.add(Status.MSG, "注销失败！");
		}
		return record;
	}
	

}
